package com.ruoyi.business.component.mqconsumer.repaymentconsumers;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.ExtMonnifyOrder;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.mapper.RepaymentOrderMapper;
import com.ruoyi.bizsys.service.IExtMonnifyOrderService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.business.service.IBRepaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 轮询的mq的处理类
 * @author wqg
 * @description
 * @date 2022/5/21
 */
@Slf4j
@Service(BizConstants.ROCKMQ_REPAYMENT_TYPE_LUNXUN)
public class LunxunConsumer implements IRepaymentMqService{

    @Autowired
    IBRepaymentService bRepaymentService;

    @Autowired
    IExtMonnifyOrderService iExtMonnifyOrderService;

    @Autowired
    RepaymentOrderMapper repaymentOrderMapper;

    /**
     * @param msgObj
     */
    @Override
    public void handle(JSONObject msgObj) {
        log.info("轮询3小时的处理类start：data:{}", msgObj.toString());
        ExtMonnifyOrder extMonnifyOrder =  msgObj.getObject("extMonnifyOrder",ExtMonnifyOrder.class);
        RepaymentOrder repaymentOrder = msgObj.getObject("repaymentOrder",RepaymentOrder.class);
        BigDecimal amountPaid = msgObj.getBigDecimal("amountPaid");
        Date time = msgObj.getDate("time");
        String reduceSource =  msgObj.getString("reduceSource");

        repaymentOrder = repaymentOrderMapper.getDetail(repaymentOrder.getRepaymentNo());
        if(null!=repaymentOrder && repaymentOrder.getRepayStatus()==BizConstants.REPAYMENT_ORDER_STATUS_SUCCESS){
            log.info("repaymentOrder.getRepayStatus():"+repaymentOrder.getRepayStatus());
            return;
        }

        bRepaymentService.transactionStatusSuccess(repaymentOrder,amountPaid, time, reduceSource);
        iExtMonnifyOrderService.updateExtMonnifyOrder(extMonnifyOrder);

        log.info("轮询3小时的处理类end：data:{}",msgObj);

    }
}
